﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized; 
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using Common;

public partial class insurance_ordercompleted : MemberPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    { 
        Response.Expires = -1;

        if (!IsPostBack)
        {
            AlipayReturn();
        }
    }
    #region 处理支付宝回传值
    void AlipayReturn()
    {
        SortedDictionary<string, string> sPara = GetRequestGet();
        if (sPara.Count > 0) //判断是否有带返回参数
        {
            AlipayNotify aliNotify = new AlipayNotify();
            bool verifyResult = aliNotify.Verify(sPara, Request.QueryString["notify_id"], Request.QueryString["sign"]);

            if (verifyResult)
            {
                /// 验证付款信息完成, 创建付款记录
                string ordernumber = Request.QueryString["out_trade_no"];
                BLL.Insurance.InsurancePolicyBase.InsertPaymentInfo(new Model.Insurance.InsurancePolicyPaymentBase("支付宝", "alipay", Request.QueryString["buyer_email"], Request.QueryString["trade_no"], Request.QueryString["trade_status"], DateTime.Now, ordernumber, "申根旅行险"));

                //调用出单系统接口，生成保单，发送至客户Email，发送短信提醒
                string cardno = "";
                string res = BLL.Insurance.InsurancePolicyBase.UpdateStatus(ordernumber, this.UserId, 1);
                if (res == "succeeded")
                {
                    this.warning.Style.Add("display", "none");
                    this.success.Style.Add("display", "");
                    lblCardNo.Text = cardno; 
                }
                else
                {
                    this.success.Style.Add("display", "none");
                    this.warning.Style.Add("display", "");
                    this.warning.InnerHtml = res;
                } 
            }
            else//验证失败
            {
                this.success.Style.Add("display", "none");
                this.warning.Style.Add("display", "");
                this.warning.InnerHtml = "支付宝返回参数验证失败";
            }
        }
        else
        {
            this.success.Style.Add("display", "none");
            this.warning.Style.Add("display", "");
            this.warning.InnerHtml = "支付宝无返回参数";
        }
    }

    /// <summary>
    /// 获取支付宝GET过来通知消息，并以“参数名=参数值”的形式组成数组
    /// </summary>
    /// <returns>request回来的信息组成的数组</returns>
    public SortedDictionary<string, string> GetRequestGet()
    {
        int i = 0;
        SortedDictionary<string, string> sArray = new SortedDictionary<string, string>();
        NameValueCollection coll;
        //Load Form variables into NameValueCollection variable.
        coll = Request.QueryString;

        // Get names of all forms into a string array.
        String[] requestItem = coll.AllKeys;

        for (i = 0; i < requestItem.Length; i++)
        {
            sArray.Add(requestItem[i], Request.QueryString[requestItem[i]]);
        }

        return sArray;
    }
    #endregion
}